"""role_permission

Revision ID: d4e6c3f32e60
Revises: f3b62c51d450
Create Date: 2024-03-20 14:56:59.389686

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision: str = 'd4e6c3f32e60'
down_revision: Union[str, None] = 'f3b62c51d450'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('system_role',
    sa.Column('name', sa.VARCHAR(length=128), nullable=True, comment='名字'),
    sa.Column('sort', sa.Integer(), server_default='1', nullable=True, comment='排序'),
    sa.Column('status', sa.Boolean(), nullable=True, comment='角色状态'),
    sa.Column('admin', sa.Boolean(), nullable=True, comment='是否为admin'),
    sa.Column('remark', sa.VARCHAR(length=1024), nullable=True, comment='备注'),
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('create_time', sa.DateTime(), server_default=sa.text('now()'), nullable=True, comment='创建时间'),
    sa.Column('update_time', sa.DateTime(), server_default=sa.text('now()'), nullable=True, comment='更新时间'),
    sa.Column('is_delete', sa.Integer(), server_default='0', nullable=True, comment='逻辑删除:0=未删除,1=删除'),
    sa.PrimaryKeyConstraint('id'),
    comment='角色表'
    )
    op.create_index(op.f('ix_system_role_id'), 'system_role', ['id'], unique=False)
    op.create_table('system_role_permission',
    sa.Column('permission_id', sa.Integer(), nullable=True),
    sa.Column('role_id', sa.Integer(), nullable=True),
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('create_time', sa.DateTime(), server_default=sa.text('now()'), nullable=True, comment='创建时间'),
    sa.Column('update_time', sa.DateTime(), server_default=sa.text('now()'), nullable=True, comment='更新时间'),
    sa.Column('is_delete', sa.Integer(), server_default='0', nullable=True, comment='逻辑删除:0=未删除,1=删除'),
    sa.ForeignKeyConstraint(['permission_id'], ['system_permission.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['role_id'], ['system_role.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id'),
    comment='角色权限表'
    )
    op.create_index(op.f('ix_system_role_permission_id'), 'system_role_permission', ['id'], unique=False)
    op.create_table('system_user_role',
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.Column('role_id', sa.Integer(), nullable=True),
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('create_time', sa.DateTime(), server_default=sa.text('now()'), nullable=True, comment='创建时间'),
    sa.Column('update_time', sa.DateTime(), server_default=sa.text('now()'), nullable=True, comment='更新时间'),
    sa.Column('is_delete', sa.Integer(), server_default='0', nullable=True, comment='逻辑删除:0=未删除,1=删除'),
    sa.ForeignKeyConstraint(['role_id'], ['system_role.id'], ondelete='CASCADE'),
    sa.ForeignKeyConstraint(['user_id'], ['system_users.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id'),
    comment='用户角色表'
    )
    op.create_index(op.f('ix_system_user_role_id'), 'system_user_role', ['id'], unique=False)
    op.alter_column('system_users', 'register_time',
               existing_type=mysql.DATETIME(),
               comment='注册时间',
               existing_comment='注册事件',
               existing_nullable=True)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('system_users', 'register_time',
               existing_type=mysql.DATETIME(),
               comment='注册事件',
               existing_comment='注册时间',
               existing_nullable=True)
    op.drop_index(op.f('ix_system_user_role_id'), table_name='system_user_role')
    op.drop_table('system_user_role')
    op.drop_index(op.f('ix_system_role_permission_id'), table_name='system_role_permission')
    op.drop_table('system_role_permission')
    op.drop_index(op.f('ix_system_role_id'), table_name='system_role')
    op.drop_table('system_role')
    # ### end Alembic commands ###
